import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.metal.MetalLookAndFeel;

import com.bnc.comps.InternalFramePanel;
import com.bnc.comps.MainMenuBar;
import com.bnc.comps.MainToolBar;
import com.bnc.comps.SplashPanel;
import com.bnc.images.ImageHelper;
import com.bnc.impl.IBNCApp;
import com.bnc.impl.IBNCDesk;
import com.bnc.props.AppProperties;
import com.bnc.props.GUIProperties;
import com.jtattoo.plaf.AbstractLookAndFeel;
import com.jtattoo.plaf.acryl.AcrylLookAndFeel;


public class BNCDesk extends JFrame implements IBNCDesk
{
	private static GUIProperties guiProps;
	private static IBNCApp app;
    
    private Rectangle appBounds;
    private BNCDesk desk;
    
    private MainMenuBar menuBar;
    private MainToolBar toolBar;
    private InternalFramePanel internalFramePanel;
    private JPanel contentPanel;
	
	public BNCDesk( IBNCApp parent )
	{
		super( AppProperties.APP_TITLE );
		
		app = parent;
		guiProps = new GUIProperties();
		guiProps.applyLookAndFeel();
		
		appBounds = AppProperties.BOUNDS_APP_FULL;
		
		setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
		
		init();
	}
	
	public BNCDesk( Rectangle bounds )
	{
		super( AppProperties.APP_TITLE );
		appBounds = bounds;
        init();
	}
	
	public void performExit() 
	{
        System.exit(0);
    }
	
	public void init() 
	{
        initModel();
        initMenuBar();
        initToolBar();
        initInternalFramePanel();
        initContentPane();
        initListeners();

        showApp();
	}
	
	private void initModel() {}

    private void initMenuBar() 
    {
        menuBar = new MainMenuBar( this );
        setJMenuBar(menuBar);
    }

    private void initToolBar() 
    {
        toolBar = new MainToolBar( this );
    }

    private void initInternalFramePanel()
    {
    	internalFramePanel = new InternalFramePanel();
    	internalFramePanel.setMaximumSize( AppProperties.SIZE_SCREEN );
    }
    
    private void initContentPane() 
    {
        contentPanel = new JPanel( new BorderLayout() );
        contentPanel.add( "North", toolBar );
        contentPanel.add ( "Center", internalFramePanel );
        
        setContentPane( contentPanel );
    }
	
    private void initListeners() {
        addWindowListener( new WindowAdapter() {

            public void windowClosing( WindowEvent e ) {
                performExit();
            }
        });
    }
    
    private void showApp() 
    {
    	try
		{
			Thread.sleep( 1000 );
		} 
    	catch ( InterruptedException e )
		{
			e.printStackTrace();
		}
        setIconImage( ImageHelper.loadImage("logo.gif").getImage() );
        setBounds( appBounds );
    }
    
    public GUIProperties getGuiProps() 
    {
        return guiProps;
    }
    
    public InternalFramePanel getInternalFramePanel()
	{
		return internalFramePanel;
	}
    
    public void updateLookAndFeel( String lf ) 
    {
        try 
        {
            LookAndFeel oldLAF = UIManager.getLookAndFeel();
            boolean oldDecorated = false;
            if ( oldLAF instanceof MetalLookAndFeel ) 
            {
                oldDecorated = true;
            }
            if (oldLAF instanceof AbstractLookAndFeel) 
            {
                oldDecorated = AbstractLookAndFeel.getTheme().isWindowDecorationOn();
            }

            if ( lf.equals( guiProps.PLAF_METAL ) ) 
            {
                javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme());
            } 
            else if ( lf.equals( guiProps.PLAF_FAST) ) 
            {
                com.jtattoo.plaf.fast.FastLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_GRAPHITE) ) 
            {
                com.jtattoo.plaf.graphite.GraphiteLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_SMART) ) 
            {
                com.jtattoo.plaf.smart.SmartLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_ACRYL) ) 
            {
                com.jtattoo.plaf.acryl.AcrylLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_AERO) ) 
            {
                com.jtattoo.plaf.aero.AeroLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_BERNSTEIN) ) 
            {
                com.jtattoo.plaf.bernstein.BernsteinLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_ALUMINIUM) ) 
            {
                com.jtattoo.plaf.aluminium.AluminiumLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_MCWIN) ) 
            {
                com.jtattoo.plaf.mcwin.McWinLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_MINT) ) 
            {
                com.jtattoo.plaf.mint.MintLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_HIFI) ) 
            {
                com.jtattoo.plaf.hifi.HiFiLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_NOIRE) ) 
            {
                com.jtattoo.plaf.noire.NoireLookAndFeel.setTheme("Default");
            } 
            else if ( lf.equals( guiProps.PLAF_LUNA) ) 
            {
                com.jtattoo.plaf.luna.LunaLookAndFeel.setTheme("Default");
            }
            guiProps.setTheme("Default");
            guiProps.setLookAndFeel(lf);
            UIManager.setLookAndFeel(guiProps.getLookAndFeel());

            LookAndFeel newLAF = UIManager.getLookAndFeel();
            boolean newDecorated = false;
            if ( newLAF instanceof MetalLookAndFeel ) 
            {
                newDecorated = true;
            }
            if ( newLAF instanceof AbstractLookAndFeel ) 
            {
                newDecorated = AbstractLookAndFeel.getTheme().isWindowDecorationOn();
            }
            if ( oldDecorated != newDecorated ) 
            {
                Rectangle savedBounds = getBounds();
                dispose();
                desk = new BNCDesk(savedBounds);
                desk.setBounds( savedBounds );
            } 
            else 
            {
                getRootPane().updateUI();
                SwingUtilities.updateComponentTreeUI( this );
                menuBar.updateLookAndFeel();
            }
        } 
        catch ( Exception ex ) 
        {
            System.out.println("Failed loading L&F: " + guiProps.getLookAndFeel() + " Exception: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public void updateTheme( String theme ) 
    {
        if ( theme != null ) {
            try {
                guiProps.setTheme(theme);
                LookAndFeel laf = UIManager.getLookAndFeel();
                if ( laf instanceof com.jtattoo.plaf.fast.FastLookAndFeel ) 
                {
                    com.jtattoo.plaf.fast.FastLookAndFeel.setTheme(theme);
                }
                if ( laf instanceof com.jtattoo.plaf.graphite.GraphiteLookAndFeel ) 
                {
                    com.jtattoo.plaf.graphite.GraphiteLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.smart.SmartLookAndFeel ) 
                {
                    com.jtattoo.plaf.smart.SmartLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.acryl.AcrylLookAndFeel ) 
                {
                    com.jtattoo.plaf.acryl.AcrylLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.aero.AeroLookAndFeel ) 
                {
                    com.jtattoo.plaf.aero.AeroLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.bernstein.BernsteinLookAndFeel ) 
                {
                    com.jtattoo.plaf.bernstein.BernsteinLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.aluminium.AluminiumLookAndFeel ) 
                {
                    com.jtattoo.plaf.aluminium.AluminiumLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.mcwin.McWinLookAndFeel ) 
                {
                    com.jtattoo.plaf.mcwin.McWinLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.mint.MintLookAndFeel ) 
                {
                    com.jtattoo.plaf.mint.MintLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.hifi.HiFiLookAndFeel ) 
                {
                    com.jtattoo.plaf.hifi.HiFiLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.noire.NoireLookAndFeel ) 
                {
                    com.jtattoo.plaf.noire.NoireLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof com.jtattoo.plaf.luna.LunaLookAndFeel ) 
                {
                    com.jtattoo.plaf.luna.LunaLookAndFeel.setTheme(theme);
                } 
                else if ( laf instanceof javax.swing.plaf.metal.MetalLookAndFeel ) 
                {
                    javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme());
                }

                UIManager.setLookAndFeel( guiProps.getLookAndFeel() );
                getRootPane().updateUI();
                SwingUtilities.updateComponentTreeUI( this );
                menuBar.updateLookAndFeel();
            } 
            catch (Exception ex) 
            {
                System.out.println("Failed setting theme! Exception: " + ex.getMessage());
                ex.printStackTrace();
            }
        }
    }
}
